<!--  -->
<template>
	<div>
		<CustomDialog
			:title="state.dialog.title"
			v-model:visible="state.dialog.isShowDialog"
			dialogWidth="900px"
			@close="handleClose"
			:dialogFooterBtn="true"
		>
			<el-form ref="goodsStockFormRef" :model="state.ruleForm" size="default" label-width="120">
				<el-form-item label="当前库存:" prop="currentStock">
					<el-input v-model="state.ruleForm.currentStock" :disabled="true" />
				</el-form-item>

				<el-form-item label="预警库存:" prop="warningStock">
					<el-input v-model="state.ruleForm.warningStock" :disabled="true" />
				</el-form-item>
				<el-form-item label="商品库存:" prop="stock" :rules="{ required: true, message: '商品标库存不能为空', trigger: 'blue' }">
					<el-input v-model="state.ruleForm.stock" type="number" min="0" />
				</el-form-item>
			</el-form>

			<template #footer>
				<el-button @click="handleClose" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit(goodsStockFormRef)" size="default">{{ state.dialog.submitTxt }} </el-button>
			</template>
		</CustomDialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { goodsStockWarningApi } from '/@/api/goods/stockwarning/index';
import msg from '/@/utils/messageUtils';
const emits = defineEmits(['refresh']);

const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '补充库存',
		submitTxt: '确定',
	},

	ruleForm: {
		id: '' as string,
		stock: 0 as number,
		currentStock: 0,
		warningStock: 0,
	},
});

const goodsStockFormRef = ref();
const handleClose = () => {
	state.dialog.isShowDialog = false;
};

const onOpen = (row: any) => {
	state.ruleForm.id = row.id;
	state.ruleForm.currentStock = row.stock;
	state.ruleForm.warningStock = row.stockWarning;
	state.dialog.isShowDialog = true;
};

const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate(async (valid) => {
		if (valid) {
			try {
				await goodsStockWarningApi().update(state.ruleForm);
				msg().success('操作成功');
				handleClose();
				emits('refresh');
			} catch (error) {
				msg().error('操作失败,请重试');
			}
		}
	});
};

defineExpose({ onOpen });
</script>

<style scoped lang="scss"></style>
